//package cn.nfwx.tv.controller;
//
//import android.app.Activity;
//import android.os.Message;
//import android.support.v4.widget.SwipeRefreshLayout;
//import android.support.v7.widget.LinearLayoutManager;
//import android.support.v7.widget.OrientationHelper;
//import android.support.v7.widget.RecyclerView;
//import android.text.TextUtils;
//import android.util.Log;
//import android.view.View;
//import android.view.ViewGroup;
//import android.view.animation.Animation;
//import android.view.animation.AnimationUtils;
//import android.widget.CompoundButton;
//import android.widget.ImageView;
//import android.widget.LinearLayout;
//import android.widget.TextView;
//import android.widget.Toast;
//
//import com.google.gson.Gson;
//
//import org.greenrobot.eventbus.EventBus;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import cn.nfwx.tv.R;
//import cn.nfwx.tv.adapter.AppiontmentAdapter;
//import cn.nfwx.tv.base.NFWXApplication;
//import cn.nfwx.tv.callback.AppointmentCallBack;
//import cn.nfwx.tv.callback.FinishListener;
//import cn.nfwx.tv.callback.ResponseListener;
//import cn.nfwx.tv.conts.Constants;
//import cn.nfwx.tv.dao.AppiontmentDao;
//import cn.nfwx.tv.events.PlayEvents;
//import cn.nfwx.tv.events.PlayLiveEvents;
//import cn.nfwx.tv.helper.AccountHelper;
//import cn.nfwx.tv.manager.AppointmentManager;
//import cn.nfwx.tv.model.AppiontmentModel;
//import cn.nfwx.tv.util.NetworkUtil;
//import cn.nfwx.tv.util.RecyclerViewUtil;
//import cn.nfwx.tv.util.SharePrefUtil;
//import cn.nfwx.tv.util.TimeUtil;
//import cn.nfwx.tv.vo.AppiontmentSection;
//import cn.nfwx.tv.vo.AppointDelete;
//import cn.nfwx.tv.vo.DeleteData;
//import cn.nfwx.tv.vo.ResultErrorVo;
//
//
///**
// * Created by dell on 2017/11/20.
// */
//
//public class AppointmentViewController implements View.OnClickListener, AppiontmentAdapter.OnDownLoadClickListener, SwipeRefreshLayout.OnRefreshListener, AppiontmentAdapter.OnCheckedChangeDeleteListener, AppiontmentAdapter.OnItemClickListener {
//    private final static String TAG_LOG = AppointmentViewController.class.getSimpleName();
//    public static final int APPOINT_STATUS_DOWNLOADED = 5;//下载已完成
//    public static final int APPOINT_STATUS_DOWNLOADING = 4;//下载中
//    public static final int APPOINT_STATUS_NORMAL = 3;//预约没开始
//    private RecyclerView recyclerView;
//    private TextView appiontment_edit_btn, delete_btn, cancle_all;
//    private LinearLayout bottom_layout;
//    private boolean flag = true;
//    private boolean isFull = false;
//    private AppiontmentAdapter adapter;
//    private Activity activity;
//    private List<AppiontmentSection> mData = new ArrayList<AppiontmentSection>();
//    private List<AppiontmentModel> mList = new ArrayList<AppiontmentModel>();
//    private Map<String, Boolean> map;
//    private SwipeRefreshLayout appiontment_refreshLayout;
//    private int pageNo = 1;
//    private int pageSize = 50;
//    private View notDataView;
//    private View errorView;
//    private ImageView empty_image, error_image;
//    private TextView empty_text, error_text;
//    private LinearLayout error_refresh_btn;
//    private static final int NETWORK_ERROR_MSG = 1;
//
//
//    public AppointmentViewController(Activity activity, View view) {
//        this.activity = activity;
//        initView(view);
//    }
//
//    public void initView(View view) {
//        bottom_layout = findView(view, R.id.bottom_layout);
//        appiontment_refreshLayout = findView(view, R.id.appiontment_refreshLayout);
//        cancle_all = findView(view, R.id.cancle_all);
//        delete_btn = findView(view, R.id.delete_btn);
//        appiontment_edit_btn = findView(view, R.id.appiontment_edit_btn);
//        recyclerView = findView(view, R.id.appiontment_recyclerView);
//        errorView = activity.getLayoutInflater().inflate(R.layout.dialog_network_error, (ViewGroup) recyclerView.getParent(), false);
//        notDataView = activity.getLayoutInflater().inflate(R.layout.view_nodata, (ViewGroup) recyclerView.getParent(), false);
//        error_image = errorView.findViewById(R.id.error_image);
//        error_text = errorView.findViewById(R.id.error_text);
//        empty_image = notDataView.findViewById(R.id.empty_image);
//        empty_text = notDataView.findViewById(R.id.empty_text);
//        error_refresh_btn = errorView.findViewById(R.id.error_refresh_btn);
//        empty_image.setImageResource(R.mipmap.empty_order);
//        empty_text.setText("暂无预约");
//
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
//        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
//        recyclerView.setLayoutManager(linearLayoutManager);
//        //recyclerView.addItemDecoration(new RecycleViewDivider(activity,LinearLayoutManager.HORIZONTAL,R.drawable.divider_null));
//        RecyclerViewUtil.setLinearLayoutManagerWithoutDivder(activity, recyclerView);
//        adapter = new AppiontmentAdapter(mData);
//        adapter.setOnDownLoadClickListener(this);
//        adapter.setOnItemClickListener(this);
//        appiontment_edit_btn.setOnClickListener(this);
//        cancle_all.setOnClickListener(this);
//        delete_btn.setOnClickListener(this);
//        error_refresh_btn.setOnClickListener(this);
//        appiontment_refreshLayout.setOnRefreshListener(this);
////        adapter.setLoadMoreView(new CustomLoadMoreView());
////        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
////            @Override
////            public void onLoadMoreRequested() {
////
////                setMoreData();
////            }
////        });
//
//        adapter.setOnCheckedChangeDeleteListener(this);
//        recyclerView.setAdapter(adapter);
//    }
//
//    public void setData() {
//        pageNo = 1;
//        if (map == null) {
//            map = new HashMap<>();
//        } else {
//            map.clear();
//        }
//        mList.clear();
//        mData.clear();
//        AppointmentManager.requestMyAppointmentList(activity, pageNo, pageSize, SharePrefUtil.getUser(activity).getAccess_token(), new AppointmentCallBack() {
//
//            @Override
//            public void onSuccess(final List<AppiontmentModel> list) {
//                if (list.size() > 0) {
//                    mList.addAll(list);
//                    AppiontmentDao dao = new AppiontmentDao(activity);
//                    dao.addList(list);//
//                    for (AppiontmentModel model : list) {
//                        model.loadData();
//                    }
//
//                    for (int i = 0; i < list.size(); i++) {
//                        if (TimeUtil.getDateCount(list.get(i).getTime().getTime()) < 7) {
//                            String MMdd;
//                            if (TimeUtil.isThisTime(list.get(i).getTime())) {
//                                MMdd = "今天";
//                            } else {
//                                MMdd = TimeUtil.getFormatMMddDate(list.get(i).getTime());
//                            }
//                            Log.d("MMdd", MMdd);
//                            if (!map.containsKey(MMdd)) {
//                                map.put(MMdd, true);
//                                mData.add(new AppiontmentSection(true, MMdd));
//                                mData.add(new AppiontmentSection(list.get(i)));
//                            } else {
//                                mData.add(new AppiontmentSection(list.get(i)));
//                            }
//                        }
//
//                    }
//                } else {
//                    activity.runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            adapter.setEmptyView(notDataView);
//                        }
//                    });
//                    // adapter.setEmptyView(notDataView);
//                }
//                pageNo++;
//                activity.runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        appiontment_refreshLayout.setRefreshing(false);
//                        adapter.setNewData(mData);
//                        adapter.notifyDataSetChanged();
//                        showSelected(mData);
//                        if (list.size() < pageSize) {
//                            adapter.loadMoreEnd(true);
//                        }
//                    }
//                });
//
//            }
//
//            @Override
//            public void onError(String requestTag, Throwable t) {
//                activity.runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        if (!NetworkUtil.hasNetwork(activity)) {
//                            error_image.setImageResource(R.mipmap.player_nointernet);
//                            error_text.setText("世界上最遥远的距离就是断网");
//                        } else {
//                            error_image.setImageResource(R.mipmap.player_fail);
//                            error_text.setText("加载失败了，点我重试");
//                        }
//                        adapter.setEmptyView(errorView);
//                        appiontment_refreshLayout.setRefreshing(false);
//                    }
//                });
//
//            }
//
//            @Override
//            public void onFail(String requestTag, String response) {
//                Gson gson = new Gson();
//                ResultErrorVo resultErrorVo = gson.fromJson(response, ResultErrorVo.class);
//                if ("AUTH".equals(resultErrorVo.getCode())) {
//                    NFWXApplication.getInstance().refreshTokenData(new FinishListener() {
//                        @Override
//                        public void onFinish() {
//                            setData();
//                        }
//                    });
//                }
//            }
//        });
//    }
//
//    /***
//     * 选中当前直播item
//     * @param mData
//     */
//    public void showSelected(List<AppiontmentSection> mData) {
//        boolean allNeverStart = true;
//        for (int i = 0; i < mData.size(); i++) {
//            AppiontmentSection appiontmentSection = mData.get(i);
//            if (appiontmentSection.t instanceof AppiontmentModel) {
//                AppiontmentModel item = (AppiontmentModel) appiontmentSection.t;
//                if (item.getTime().getTime() < TimeUtil.getTime() * 1000L) {
//                    allNeverStart = false;
//                }
//                if (item.getStatus() == 2) {
//                    recyclerView.scrollToPosition(i);
//                    return;
//                } else {
//                    if (i > 1 && mData.get(i - 1).t instanceof AppiontmentModel && mData.get(i).t instanceof AppiontmentModel) {
//                        AppiontmentModel itemPre = (AppiontmentModel) mData.get(i - 1).t;
//                        if (itemPre.getTime().getTime() < TimeUtil.getTime() * 1000L && item.getTime().getTime() > TimeUtil.getTime() * 1000L) {
//                            recyclerView.scrollToPosition(i);
//                        } else {
//                            recyclerView.scrollToPosition(mData.size() - 1);
//                        }
//                    } else if (i > 2 && mData.get(i - 2).t instanceof AppiontmentModel && mData.get(i).t instanceof AppiontmentModel) {
//                        AppiontmentModel itemPre2 = (AppiontmentModel) mData.get(i - 2).t;
//                        if (itemPre2.getTime().getTime() < TimeUtil.getTime() * 1000L && item.getTime().getTime() > TimeUtil.getTime() * 1000L) {
//                            recyclerView.scrollToPosition(i);
//                        } else {
//                            recyclerView.scrollToPosition(mData.size() - 1);
//                        }
//                    }
//                }
//            }
//        }
//        if (allNeverStart) {
//            recyclerView.scrollToPosition(0);
//        }
//
//    }
//
//
//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.appiontment_edit_btn:
//                if (flag) {
//                    bottom_layout.setVisibility(View.VISIBLE);
//                    appiontment_edit_btn.setText("取消");
//                    adapter.setDelete(true);
//                } else {
//                    bottom_layout.setVisibility(View.GONE);
//                    appiontment_edit_btn.setText("编辑");
//                    adapter.getDeleteData().clear();//清空选中数据
//                    adapter.setDelete(false);
//                }
//                flag = !flag;
//                break;
//            case R.id.delete_btn:
//                Map<Integer, String> map = adapter.getDeleteData();
//                List<DeleteData> deleteDataList = new ArrayList<DeleteData>();
//                AppointDelete appointDelete = new AppointDelete();
//
//                for (Integer id : map.keySet()) {
//                    DeleteData app = new DeleteData();
//                    app.setTime(((AppiontmentModel) adapter.getData().get(id).t).getTime());
//                    app.setLid(((AppiontmentModel) adapter.getData().get(id).t).getLid());
//                    AppiontmentDao dao = new AppiontmentDao(activity);
//                    List<AppiontmentModel> list = dao.query(((AppiontmentModel) adapter.getData().get(id).t).getLid(), ((AppiontmentModel) adapter.getData().get(id).t).getTime());
//                    if (list.size() > 0) {
//                        dao.delete(list.get(0));
//                    }
//                    deleteDataList.add(app);
//
//                }
//                appointDelete.setApps(deleteDataList);
//                Gson gson = new Gson();
//                Log.d(TAG_LOG, gson.toJson(appointDelete));
//                Log.d(TAG_LOG, SharePrefUtil.getUser(activity).getAccess_token());
//                AppointmentManager.cancelAppointment(activity, gson.toJson(appointDelete), SharePrefUtil.getUser(activity).getAccess_token(), new ResponseListener<String>() {
//                    @Override
//                    public void onResponse(String requestTag, final String response) {
//                        activity.runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                if ("true".equals(response)) {
//                                    bottom_layout.setVisibility(View.GONE);
//                                    appiontment_edit_btn.setText("编辑");
//                                    setDeleteDataChange(adapter.getDeleteData());
//                                    adapter.getDeleteData().clear();//清空选中数据
//                                    adapter.setDelete(false);
//                                    adapter.notifyDataSetChanged();
//                                }
//                            }
//                        });
//                    }
//
//                    @Override
//                    public void onError(String requestTag, Throwable t) {
//                        Log.d(TAG_LOG, t.getMessage());
//                        activity.runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                Toast.makeText(activity, "删除失败", Toast.LENGTH_LONG).show();
//                            }
//                        });
//                    }
//
//                    @Override
//                    public void onFinish() {
//
//                    }
//                });
//
//                break;
//            case R.id.cancle_all:
//                if (isFull) {
//                    adapter.getDeleteData().clear();
//                    adapter.notifyDataSetChanged();
//                    isFull = false;
//                    cancle_all.setText("全选");
//                } else {
//                    for (int i = 0; i < adapter.getData().size(); i++) {
//                        if (!adapter.getData().get(i).isHeader) {
//                            if (!adapter.getDeleteData().containsKey(i)) {
//
//                                adapter.getDeleteData().put(i, ((AppiontmentModel) adapter.getData().get(i).t).getLid());
//                            }
//                        }
//
//                    }
//                    cancle_all.setText("取消全选");
//                    adapter.notifyDataSetChanged();
//                    isFull = true;
//                }
//                break;
//            case R.id.error_refresh_btn:
//                error_image.setImageResource(R.drawable.footer_loading_progress);
//                Animation rotate = AnimationUtils.loadAnimation(activity, R.anim.rotate_anim);
//                error_image.startAnimation(rotate);
//                mHandler.sendEmptyMessageDelayed(NETWORK_ERROR_MSG, 1000);
//                break;
//        }
//    }
//
//    private android.os.Handler mHandler = new android.os.Handler() {
//        @Override
//        public void dispatchMessage(Message msg) {
//            super.dispatchMessage(msg);
//            switch (msg.what) {
//                case NETWORK_ERROR_MSG:
//                    setData();
//                    break;
//            }
//        }
//    };
//
//
//    /**
//     * 查找控件
//     *
//     * @param rid
//     * @param <T>
//     * @return
//     */
//    public <T extends View> T findView(View view, int rid) {
//        return (T) view.findViewById(rid);
//    }
//
//
//    @Override
//    public void onOnItemClick(View view, final int position, final AppiontmentModel model) {
////        DownLoadManager.download(activity, model.getPlayPath(), new Interfaces.OnFileDownloadCreatedListener() {
////            @Override
////            public void onCreated(String vsid) {
////                activity.runOnUiThread(new Runnable() {
////                    @Override
////                    public void run() {
////                        Log.d("status", model.getStatus() + "");
////                        model.setStatus(4);
////                        adapter.notifyDataSetChanged();
////                        Toast.makeText(activity, "已加入下载队列", Toast.LENGTH_LONG).show();
////
////                    }
////                });
////            }
////        }, new Interfaces.OnFileDownloadFailedListener() {
////            @Override
////            public void onFailed(String vsid) {
////                Toast.makeText(activity, "下载失败", Toast.LENGTH_LONG).show();
////            }
////        });
//    }
//
//    @Override
//    public void onRefresh() {
//        setData();
//    }
//
//    @Override
//    public void onCheckedChanged(CompoundButton compoundButton, boolean b, int position) {
//        if (b) {
//            adapter.getDeleteData().put(position, ((AppiontmentModel) adapter.getData().get(position).t).getLid());
//        } else {
//            adapter.getDeleteData().remove(adapter.getDeleteData().get(position));
//        }
//        if (adapter.getDeleteData().keySet().size() == mList.size()) {
//            cancle_all.setText("取消全选");//全部勾上
//            isFull = true;
//        } else {
//            cancle_all.setText("全选");//还未全部勾上
//            isFull = false;
//        }
//    }
//
//    /***
//     * 从数据源删除数据
//     * @param deleteMap
//     */
//    public void setDeleteDataChange(Map<Integer, String> deleteMap) {
//        for (Integer id : deleteMap.keySet()) {
//            if (!adapter.getData().get(id).isHeader) {
//                AppiontmentDao dao = new AppiontmentDao(activity);
//                dao.delete((AppiontmentModel) adapter.getData().get(id).t);
//                mList.remove(adapter.getData().get(id).t);
//            }
//        }
//        mData.clear();
//        map.clear();
//        if (mList.size() > 0) {
//            for (int i = 0; i < mList.size(); i++) {
//                String MMdd;
//                if (TimeUtil.isThisTime(mList.get(i).getTime())) {
//                    MMdd = "今天";
//                } else {
//                    MMdd = TimeUtil.getFormatMMddDate(mList.get(i).getTime());
//                }
//                if (!map.containsKey(MMdd)) {
//                    map.put(MMdd, true);
//                    mData.add(new AppiontmentSection(true, MMdd));
//                    mData.add(new AppiontmentSection(mList.get(i)));
//                } else {
//                    mData.add(new AppiontmentSection(mList.get(i)));
//                }
//            }
//
//        } else {
//            activity.runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    adapter.setEmptyView(notDataView);
//                }
//            });
//        }
//    }
//
//    @Override
//    public void onClick(View view, int position, AppiontmentModel model) {
//        Log.d(TAG_LOG, model.getPlayPath() + "");
//        if (!TextUtils.isEmpty(model.getPlayPath())) {
//            //VodPlayerActivity.startActivity(activity, model.getPlayPath(), null, model.getName(), 0);
//            EventBus.getDefault().post(new PlayEvents(model.getPlayPath(), null, model.getName(), 0));
//            //activity.finish();
//        } else if (model.getStatus() == 2) {
//            EventBus.getDefault().post(new PlayLiveEvents(model.getLid(), model.getLiveName()));
//            activity.finish();
//        }
//    }
//}
